﻿<UserControl
  x:Name="This"
  SnapsToDevicePixels="True"
  x:Class="Kaxaml.DocumentViews.AgDocumentView"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:c="clr-namespace:Kaxaml.Controls"
  xmlns:a="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
  xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
  xmlns:forms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
  xmlns:settings="clr-namespace:Kaxaml.Properties">

  <UserControl.Resources>
    <ResourceDictionary>

      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="\Resources\Brushes.xaml" />
      </ResourceDictionary.MergedDictionaries>

      <!--
      *************************************************************************
      Data
      *************************************************************************
      -->

      <c:NotConverter x:Key="NotConverter" />
      <c:GreaterThanConverter x:Key="GreaterThanConverter" />

      <!--
      *************************************************************************
      Animations
      *************************************************************************
      -->

      <DoubleAnimation 
        x:Key="ShowErrorOverlay" 
        Storyboard.TargetName="ErrorOverlay" 
        Storyboard.TargetProperty="Opacity"
        To="1"
        Duration="0:0:0" />

      <DoubleAnimation 
        x:Key="HideErrorOverlay"
        Storyboard.TargetName="ErrorOverlay" 
        Storyboard.TargetProperty="Opacity"
        Duration="0:0:0" />

    </ResourceDictionary>
  </UserControl.Resources>

  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
      <RowDefinition Height="*" />
      <RowDefinition Height="Auto" />
      <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <!-- Content -->

    <wfi:WindowsFormsHost x:Name="FormsHost" Grid.Row="1">
      <forms:WebBrowser x:Name="ContentArea" />
    </wfi:WindowsFormsHost>

    <Image x:Name="ColorOverlayImage" Stretch="None" Grid.Row="1" />
    
    <Border x:Name="ErrorOverlay" Grid.Row="1" Opacity="0" IsHitTestVisible="False">
      <Image x:Name="ErrorOverlayImage" Stretch="None" Source="{Binding Path=XamlDocument.PreviewImage}" />
    </Border>

    <!-- Error Display -->

    <Expander SnapsToDevicePixels="True" x:Name="ErrorDisplay" Grid.Row="1" VerticalAlignment="Top" IsExpanded="{Binding ElementName=This, Path=IsValidXaml, Converter={StaticResource NotConverter}}">
      <Expander.Template>
        <ControlTemplate TargetType="{x:Type Expander}">
          <Grid ClipToBounds="True">
            <Grid x:Name="OuterContainer" Margin="0,-150,0,0" VerticalAlignment="Top">
              <Border
                x:Name="OuterBorder"
                Margin="0,0,0,4" 
                VerticalAlignment="Top"
                BorderBrush="#909090"
                BorderThickness="0,1,0,0"
                SnapsToDevicePixels="True">
                <Border.Background>
                  <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                    <GradientStop Color="#787878" Offset="0" />
                    <GradientStop Color="#585858" Offset="1" />
                  </LinearGradientBrush>
                </Border.Background>
                <Border
                  BorderBrush="#333"
                  BorderThickness="0,0,0,1">
                  <ContentPresenter Margin="0,0,0,-1" />
                </Border>
              </Border>
              <Rectangle VerticalAlignment="Bottom" Height="4">
                <Rectangle.Fill>
                  <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                    <GradientStop Offset="0" Color="#6000" />
                    <GradientStop Offset="1" Color="#0000" />
                  </LinearGradientBrush>
                </Rectangle.Fill>
              </Rectangle>
            </Grid>
          </Grid>
          <ControlTemplate.Triggers>
            <Trigger Property="IsExpanded" Value="True">
              <Trigger.EnterActions>
                <BeginStoryboard>
                  <Storyboard>
                    <ThicknessAnimation DecelerationRatio="0.9" Storyboard.TargetName="OuterContainer" Storyboard.TargetProperty="Margin" BeginTime="0:0:0.15" Duration="0:0:0.15" To="0,0,0,0" />
                  </Storyboard>
                </BeginStoryboard>
              </Trigger.EnterActions>
              <Trigger.ExitActions>
                <BeginStoryboard>
                  <Storyboard>
                    <ThicknessAnimation AccelerationRatio="0.9" Storyboard.TargetName="OuterContainer" Storyboard.TargetProperty="Margin" Duration="0:0:0.15"  />
                  </Storyboard>
                </BeginStoryboard>
              </Trigger.ExitActions>
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Expander.Template>

      <Grid>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="Auto" />
          <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
          <RowDefinition Height="Auto" />
          <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <!-- Exclamation -->
        <TextBlock
            Grid.Column="0"
            Grid.RowSpan="2"
            FontWeight="Bold"
            Foreground="#FFFFBF00"
            FontFamily="Courier New"
            FontSize="44"
            Text="!"
            Margin="4,0,0,0"/>

        <!-- Error Message -->
        <TextBlock
           Grid.Column="1"
           Grid.Row="0"
           Margin="4,8,4,2"
           Foreground="#EEE"
           TextWrapping="Wrap"
           Text="{Binding ElementName=This, Path=ErrorText}" />

        <!-- Line Number -->
        <Button
          Grid.Column="1"
          Grid.Row="1"
          Margin="4,0,4,7"
          Foreground="#FFFFBF00"
          Cursor="Hand"
          HorizontalAlignment="Left" 
          Click="LineNumberClick" 
          MinWidth="0"
          MinHeight="0">
          <Button.Template>
            <ControlTemplate TargetType="{x:Type Button}">
              <Border Background="Transparent">
                <StackPanel Orientation="Horizontal">
                  <ContentPresenter VerticalAlignment="Center" />
                  <Path x:Name="Arrow" Fill="White" Margin="8,0,0,0" Stretch="Fill" Opacity="0.2" VerticalAlignment="Center" Width="5" Height="9" Data="M200,200 L200,280 256,240 z"/>
                </StackPanel>
              </Border>
              <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                  <Setter TargetName="Arrow" Property="Opacity" Value="1" />
                  <Setter TargetName="Arrow" Property="Fill" Value="#FFFFBF00" />
                </Trigger>
                <MultiDataTrigger>
                  <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding ElementName=This, Path=ErrorLineNumber}" Value="0" />
                    <Condition Binding="{Binding ElementName=This, Path=ErrorLinePosition}" Value="0" />
                  </MultiDataTrigger.Conditions>
                  <Setter Property="Visibility" Value="Collapsed" />
                </MultiDataTrigger>
              </ControlTemplate.Triggers>
            </ControlTemplate>
          </Button.Template>
          <TextBlock>
            Line
            <TextBlock Text="{Binding ElementName=This, Path=ErrorLineNumber}" />,
            Position
            <TextBlock Text="{Binding ElementName=This, Path=ErrorLinePosition}" />
          </TextBlock>
        </Button>
      </Grid>
    </Expander>

    <GridSplitter
      Grid.Row="2"
      Height="6" 
      ResizeDirection="Rows"
      ResizeBehavior="PreviousAndNext"
      SnapsToDevicePixels="True"
      VerticalAlignment="Bottom"
      HorizontalAlignment="Stretch">
      <GridSplitter.Template>
        <ControlTemplate TargetType="{x:Type GridSplitter}">
          <Border Background="#20FFFFFF" />
        </ControlTemplate>
      </GridSplitter.Template>
    </GridSplitter>

    <c:KaxamlTextEditor
      x:Name="Editor"
      Grid.Row="3"
      FontFamily="{Binding Source={x:Static settings:Settings.Default}, Path=EditorFontFamily, Mode=TwoWay}"
      FontSize="{Binding Source={x:Static settings:Settings.Default}, Path=EditorFontSize, Mode=TwoWay}"
      ConvertTabs="{Binding Source={x:Static settings:Settings.Default}, Path=ConvertTabs}"
      ConvertTabsCount="{Binding Source={x:Static settings:Settings.Default}, Path=ConvertTabsCount}" 
      ShowLineNumbers="{Binding Source={x:Static settings:Settings.Default}, Path=ShowLineNumbers}" 
      EnableSyntaxHighlighting="{Binding Source={x:Static settings:Settings.Default}, Path=EnableSyntaxHighlighting}" 
      EnableXmlFolding="{Binding Source={x:Static settings:Settings.Default}, Path=EnableXmlFolding}" 
      IsCodeCompletionEnabled="{Binding Source={x:Static settings:Settings.Default}, Path=EnableCodeCompletion}" 
      Text="{Binding Path=SourceText, Mode=TwoWay}"
      TextChanged="EditorTextChanged" />

  </Grid>

</UserControl>
